package com.gambi.tapestry.client.chatter.view;

import com.gambi.tapestry.client.chatter.model.Contact;
import com.gambi.tapestry.client.chatter.model.Message;
import com.gambi.tapestry.client.chatter.widgets.TitleCommandBar;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ChatWindowView extends Composite {
	private MessengerView view;
	private Contact contactTo;
	private ScrollPanel conversationScroller = new ScrollPanel();
	private VerticalPanel conversationPanel = new VerticalPanel();
	private TextBox input = new TextBox();
	int position = -1;

	public ChatWindowView(MessengerView view, Contact contactTo) {
		this.view = view;
		this.contactTo = contactTo;
		VerticalPanel mainPanel = new VerticalPanel();
		initWidget(mainPanel);
		TitleCommandBar titleBar;
		titleBar = new TitleCommandBar(contactTo.getName());
		titleBar.addCommand("close", new ClickListener() {
			public void onClick(Widget sender) {
				ChatWindowView.this.view.closeChatWindow(ChatWindowView.this);
			}
		});
		mainPanel.add(titleBar);
		VerticalPanel vpanel = new VerticalPanel();
		vpanel.setHeight("100%");
		vpanel.setWidth("100%");
		mainPanel.add(vpanel);
		mainPanel.setCellHeight(vpanel, "100%");
		conversationScroller.setHeight("100%");
		vpanel.add(conversationScroller);
		vpanel.setCellHeight(conversationScroller, "100%");
		conversationScroller.setWidget(conversationPanel);
		vpanel.add(input);
		input.setWidth("100%");
		input.addKeyboardListener(new InputListener());
		RootPanel.get().add(this);
	}

	private class InputListener extends KeyboardListenerAdapter {
		public void onKeyPress(Widget sender, char keyCode, int modifiers) {
			if (keyCode == KEY_ENTER) {
				String text = input.getText();
				if (text.length() > 0) {
					Message message = new Message(text);
					addMessage(view.getContactList().getMe(), message);
					view.getListener().onSendMessage(contactTo, message);
					input.setText("");
				}
			}
		}
	}

	public void addMessage(Message message) {
		addMessage(contactTo, message);
	}

	public void addMessage(Contact contact, Message message) {
		HTML messageLabel = new HTML("<b>" + contact.getName() + "</b>: "
				+ message);
		conversationPanel.add(messageLabel);
		conversationScroller.setScrollPosition(conversationPanel
				.getOffsetHeight());
		messageLabel.setStyleName("convMessage");
	}

	static final int CHAT_WINDOW_WIDTH = 250;
	static final int CHAT_WINDOW_HEIGHT = 200;
	static final int CHAT_WINDOW_MARGIN = 5;
	static final int CHAT_WINDOW_OFFSET = 20;

	public void show() {
		setWidth(CHAT_WINDOW_WIDTH + "px");
		setHeight(CHAT_WINDOW_HEIGHT + "px");
		setVisible(true);
		setPosition(position);
		input.setFocus(true);
	}

	public void setPosition(int position) {
		this.position = position;
		int top = Window.getClientHeight()
				+ DOM.getIntAttribute(RootPanel.getBodyElement(), "scrollTop")
				- getOffsetHeight() - CHAT_WINDOW_MARGIN;
		int left = Window.getClientWidth()
				+ DOM.getIntAttribute(RootPanel.getBodyElement(), "scrollLeft")
				- CHAT_WINDOW_WIDTH * (position + 1) - CHAT_WINDOW_MARGIN
				* (position + 1) - CHAT_WINDOW_OFFSET;
		DOM.setStyleAttribute(getElement(), "position", "absolute");
		DOM.setStyleAttribute(getElement(), "top", Integer.toString(top));
		DOM.setStyleAttribute(getElement(), "left", Integer.toString(left));
		conversationScroller.setScrollPosition(conversationPanel
				.getOffsetHeight());
	}
}
